package com.hulk.dryad.manage.listener;

import com.hulk.dryad.manage.framework.jpush.bean.JpushMessage;
import com.hulk.dryad.manage.framework.jpush.template.JpushTemplate;
import com.hulk.dryad.manage.listener.event.MailAndJpushEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Collections;


/**
 * jpush 极光消息推送
 * @author hulk
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class JpushEventListener {

	private final ObjectProvider<JpushTemplate> jpushTemplate;

	@EventListener(MailAndJpushEvent.class)
	public void onEvent(MailAndJpushEvent event) {
		String workId = event.getWorkId();
		log.info("极光消息推送,workId:[{}] MailAndJPushEvent:[{}]", workId, event);
		JpushMessage jpush= new JpushMessage();
		jpush.setContent(event.getMessage());
		JpushTemplate template = jpushTemplate.getIfAvailable();
		Long msgId = template.pushToAliases(Collections.singletonList(event.getReceiver()), jpush);
		log.info("workId:[{}] 返回消息ID:[{}]极光消息推送完成", workId,msgId);


	}
}
